﻿using SkiaAndModel.Models;
using SkiaAndModel.NodeAbstract;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
using SkiaSharp.Views.WPF;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SkiaAndModel.Views
{
    public partial class SkiaCanvas : UserControl
    {
        // 数据模型：梯形图树
        public Tree LadderTree { get; set; } = new Tree();

        // 布局引擎
        private LadderLayoutEngine layoutEngine = new LadderLayoutEngine();

        // 用于交互：记录节点拖动
        private Guid? selectedNodeId = null;
        private SKPoint dragStartPoint;

        public SkiaCanvas()
        {
            InitializeComponent();
            // 当控件加载完成或尺寸变化时重新绘制
            skiaElement.SizeChanged += (s, e) => skiaElement.InvalidateVisual();
        }



        /// <summary>
        /// 根据节点类型绘制图形
        /// ElementNode：绘制矩形并显示名称
        /// VirtualNode：绘制圆形（或椭圆）并显示名称
        /// </summary>
        private void DrawNode(SKCanvas canvas, INode node, SKPoint pos)
        {
            float width = 80, height = 40;
            SKRect rect = new SKRect(pos.X - width / 2, pos.Y - height / 2, pos.X + width / 2, pos.Y + height / 2);
            using (var paint = new SKPaint { IsAntialias = true, StrokeWidth = 2 })
            {
                switch (node.NodeType)
                {
                    case NodeType.ElementNode:
                        paint.Color = SKColors.LightBlue;
                        canvas.DrawRect(rect, paint);
                        break;
                    case NodeType.VirtualNode:
                        paint.Color = SKColors.LightGreen;
                        canvas.DrawCircle(pos.X, pos.Y, width / 2, paint);
                        break;
                }

                // 绘制节点名称
                using (var textPaint = new SKPaint { Color = SKColors.Black, TextSize = 14, IsAntialias = true })
                {
                    canvas.DrawText(node.Name, rect.Left + 5, rect.MidY + 5, textPaint);
                }
            }
        }




     

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (selectedNodeId.HasValue && e.LeftButton == MouseButtonState.Pressed)
            {
                var pos = e.GetPosition(skiaElement);
                SKPoint currentPos = new SKPoint((float)pos.X, (float)pos.Y);
                SKPoint delta = new SKPoint(currentPos.X - dragStartPoint.X, currentPos.Y - dragStartPoint.Y);

                if (layoutEngine.NodePositions.ContainsKey(selectedNodeId.Value))
                {
                    SKPoint oldPos = layoutEngine.NodePositions[selectedNodeId.Value];
                    layoutEngine.NodePositions[selectedNodeId.Value] = new SKPoint(oldPos.X + delta.X, oldPos.Y + delta.Y);
                    dragStartPoint = currentPos;
                    skiaElement.InvalidateVisual();
                }
            }
        }

        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (selectedNodeId.HasValue)
            {
                skiaElement.ReleaseMouseCapture();
                selectedNodeId = null;
            }
        }

        private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {

        }

        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {

        }
    }
}

